package com.yanqu.road.server.cmd.activity.tongbing;

import com.yanqu.road.entity.activity.tongbing.config.TongBingConfig;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_TONG_BING_MOVE_SPEED_UP, desc = "")
public class TongBingMoveSpeedUpCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TongBingConfig config = TongBingMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!config.inEnterActivityTime()) {
            ret = GameErrorCode.E_TONG_BING_NO_OPERATE_TIME;
            return;
        }

        TongBingProto.TongBingMoveSpeedUpReqMsg reqMsg = TongBingProto.TongBingMoveSpeedUpReqMsg.parseFrom(packet.getBytes());

        // 数量校验
        int times = reqMsg.getTimes();
        if (times <= 0 || times > 9999) {
            ret = GameErrorCode.E_TONG_BING_PARAM_ERR;
            return;
        }

        // 判断道具数量
        Property property = PropertyHelper.parseStringToProperty(config.getShenXingItem() + "=" + times);
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(property);

        // 跨服执行操作
        TongBingProto.CrossTongBingMoveSpeedUpReqMsg.Builder builder = TongBingProto.CrossTongBingMoveSpeedUpReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg.toBuilder());
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_TONG_BING_MOVE_SPEED_UP, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        TongBingProto.TongBingMoveSpeedUpRespMsg.Builder respMsg = TongBingProto.TongBingMoveSpeedUpRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_TONG_BING_MOVE_SPEED_UP, respMsg);
    }
}
